#!/usr/bin/env python3
"""
演示CONNECT方法CURL转换修复效果
"""
import sys
import os
from datetime import datetime

sys.path.insert(0, os.path.dirname(__file__))

from idea_proxy import IDEAProxy
from simple_dashboard_fixed import SimpleDashboard

# 创建代理实例
proxy = IDEAProxy('127.0.0.1', 8080)

# 添加包含CONNECT方法的测试请求
test_requests = [
    {
        'timestamp': datetime.now().isoformat(),
        'client_addr': '127.0.0.1:12345',
        'method_line': 'CONNECT plugins.jetbrains.com:443 HTTP/1.1',
        'headers': {
            'User-Agent': 'Java/17.0.10',
            'Host': 'plugins.jetbrains.com',
            'Accept': 'text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2'
        },
        'raw_request': 'CONNECT plugins.jetbrains.com:443 HTTP/1.1'
    },
    {
        'timestamp': datetime.now().isoformat(),
        'client_addr': '127.0.0.1:12346',
        'method_line': 'CONNECT www.jetbrains.com:443 HTTP/1.1',
        'headers': {
            'User-Agent': 'IntelliJ IDEA/2023.3',
            'Host': 'www.jetbrains.com'
        },
        'raw_request': 'CONNECT www.jetbrains.com:443 HTTP/1.1'
    },
    {
        'timestamp': datetime.now().isoformat(),
        'client_addr': '127.0.0.1:12347',
        'method_line': 'GET https://api.github.com/user HTTP/1.1',
        'headers': {
            'User-Agent': 'IntelliJ IDEA/2023.3',
            'Host': 'api.github.com',
            'Authorization': 'token ghp_xxxxxxxxxxxx'
        },
        'raw_request': 'GET https://api.github.com/user HTTP/1.1'
    }
]

for req in test_requests:
    proxy.requests_log.append(req)

print("🎯 CONNECT方法CURL转换修复演示")
print("=" * 60)
print()
print("📊 测试数据:")
print(f"   - {len([r for r in test_requests if 'CONNECT' in r['method_line']])} 个CONNECT请求")
print(f"   - {len([r for r in test_requests if 'GET' in r['method_line']])} 个GET请求")
print(f"   - 总共 {len(test_requests)} 个请求")
print()

print("🔧 修复内容:")
print("   ✅ CONNECT plugins.jetbrains.com:443 → GET https://plugins.jetbrains.com/")
print("   ✅ CONNECT www.jetbrains.com:443 → GET https://www.jetbrains.com/")
print("   ✅ 普通HTTPS GET请求保持不变")
print("   ✅ 生成的CURL命令可以直接执行")
print()

print("💡 解决的问题:")
print("   ❌ 之前: curl -X CONNECT \"plugins.jetbrains.com:443\" ...")
print("      → 服务器返回400错误 (Bad request)")
print("   ✅ 现在: curl \"https://plugins.jetbrains.com/\" ...") 
print("      → 正常的HTTP请求，可以成功执行")
print()

# 启动dashboard
dashboard = SimpleDashboard(proxy, 8082)
dashboard.start()

print("🚀 修复后的Dashboard已启动!")
print("🌐 访问地址: http://172.18.249.85:8082 (WSL IP)")
print("📱 新功能:")
print("   • 点击'📋 复制'查看修复后的CURL命令")
print("   • CONNECT请求自动转换为对应的HTTPS请求")
print("   • 生成的命令可以直接在终端执行测试")
print()
print("🧪 测试步骤:")
print("   1. 打开dashboard页面")
print("   2. 找到CONNECT请求行")
print("   3. 点击'📋 复制'按钮")
print("   4. 查看生成的CURL命令（应该是GET https://...）")
print("   5. 复制命令到终端执行")
print()
print("按任意键退出...")
input()
dashboard.stop()
print("✅ Dashboard已停止")
print("🎉 CONNECT方法CURL转换修复完成！")